#ifndef UARTS_H
#define UARTS_H


#include <iom128.h>
#include <ina90.h>
#include <stdint.h>
#include "Util.h"
#include "UpDown.h"

void Do_PC_CMD(void);
void Do_UART_Reply(void);
void Send_UART_CMD(uint8_t adr, uint8_t len);

class USARTS
{
    GETSETBASE(uint8_t, my_adr , MyAdr);
    GETSETBASE(uint8_t, dst_adr , DstAdr);
    
    GETSETBASE(bool, flag , Flag);
    
  public:
    USARTS(uint8_t m_adr, uint8_t d_adr, void (*Do)(void));
    bool isCommandReceived(void)
    {
      return cmd_rcvd;
    };
    bool CrcCheck(void);
    void (*DoCmd)(void);
    
    void tic(void)
    {
      flag = true;
    };
    
    bool Send_UART1_CMD(uint8_t adr, uint8_t len);
    
    friend void Do_PC_CMD(void);
    friend void Do_UART_Reply(void);
    friend UP;
    friend DOWN;
    
  private:
    void USART0_Send(uint8_t data);
    void USART1_Send(uint8_t data);
    void PacketFilter(uint8_t usart_status, uint8_t RCV_data);
#pragma vector=USART0_RXC_vect
    __interrupt static void URX0();
#pragma vector=USART1_RXC_vect
    __interrupt static void URX1();
    /*
    #pragma vector=TIMER3_COMPA_vect
        __interrupt static void TIMER3();
    */
    bool cmd_rcvd;
    uint8_t recive_byte_counter;
    uint8_t uart_down_counter;
    uint8_t recive_data_counter;
    uint8_t recive_data_lenght;
    uint8_t recive_data_crc;
    uint8_t recive_hedear_crc;
    
    uint8_t recive_buffer[24];
    uint8_t transmit_buffer[24];
};


#ifdef UARTS_C

#define RW0 PORTE_Bit2
#define RW2 PORTD_Bit1

USARTS usart0(0xAA, 0xFF, Do_PC_CMD);
USARTS usart1(0xFF, 0x00, Do_UART_Reply);

#else

extern USARTS usart0;
extern USARTS usart1;

#endif


#endif